package com.koron.css2.newProjectCenter.service;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.vo.gcjs.GJformInfoVo;
import com.koron.css2.newProjectCenter.vo.gcjs.GJsqrInfoVo;
import com.koron.css2.newProjectCenter.vo.gcjs.GJsubProInfoVo;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工建请求数据封装
 * @author 张涛
 */
public class gcjsData {
    //客户类型为普通客户
    final static String CTM_TYPE = "0";

    public static Logger logger = LogManager.getLogger(gcjsData.class);

    /**
     * 封装传给工建系统的数据模型
     */
    public static String gcjsDataModel(UserInfoBean userInfo, PjInfoBean pjInfoBean, CtmInfoBean ctmInfo, AccountInfoBean accountInfo){
        JSONObject jsonObject  =new JSONObject();
        //封装工程数据模型
        GJsubProInfoVo gJsubProInfoVo = new GJsubProInfoVo();
        gJsubProInfoVo.setName(pjInfoBean.getPjName());
        gJsubProInfoVo.setCode(pjInfoBean.getBillNo());
        gJsubProInfoVo.setSubproprogress(1);//默认新建工程
        gJsubProInfoVo.setBelongstage(1);//默认立项用地规划许可
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        gJsubProInfoVo.setStarttime(sdf.format(pjInfoBean.getBillDate()));
        gJsubProInfoVo.setFinishtime(null);//无
        gJsubProInfoVo.setProjectscope(null);//无
        //封装申请人数据模型
        GJsqrInfoVo gJsqrInfoVo = new GJsqrInfoVo();
        gJsqrInfoVo.setSqr(ctmInfo.getCtmName());
        gJsqrInfoVo.setSqrlx(CTM_TYPE.equals(ctmInfo.getCtmType())?0:1);
        gJsqrInfoVo.setSqrzjlx(Integer.parseInt(getCertType(ctmInfo.getCertType())));
        gJsqrInfoVo.setSqrzjhm(ctmInfo.getCertNo());
        gJsqrInfoVo.setSqrsjhm(ctmInfo.getMobile());
        gJsqrInfoVo.setSqrlxdz(ctmInfo.getCtmAddr());
        //封装经办人数据模型
//        GJjbrinfoVo gJjbrinfoVo = new GJjbrinfoVo();
//        gJjbrinfoVo.setDbr(pjInfoBean.getAgentMan());
//        gJjbrinfoVo.setDbrzjlx(Integer.parseInt(getCertType(pjInfoBean.getAgentCertType())));
//        gJjbrinfoVo.setDbrzjhm(pjInfoBean.getAgentCertNo());
//        gJjbrinfoVo.setDbrzjhm(pjInfoBean.getAgentLinkTel());
//        gJjbrinfoVo.setDbrlxdz(null);
        //封装事项数据模型，写死
        Map<String,String> itemJson = new HashMap<>();
        itemJson.put("itemid","c5c1888f54774d178cd0ea8bdfa0ff3e");
//        itemJson.put("itemcode","91440900194921077Y3442199429000");
        itemJson.put("itemdept","信宜粤海水务有限公司");
        itemJson.put("itemname","用水报装");
        //表单数据模型
        GJformInfoVo gJformInfoVo = new GJformInfoVo();
        gJformInfoVo.setGdbsuseridcode(null);
        gJformInfoVo.setXzqhdm("440983");//行政区划
        gJformInfoVo.setBllx(1);//默认新装
        gJformInfoVo.setYsyqlx(getWaterType(pjInfoBean.getPjType()));
//        gJformInfoVo.setBlfs(2);//经办人为空则默认本人办理
        gJformInfoVo.setBlfs(StringUtils.isNotBlank(pjInfoBean.getAgentMan())?2:1);//经办人为空则默认本人办理
        gJformInfoVo.setXyzxm(ctmInfo.getCtmName());
        gJformInfoVo.setXyzdh(ctmInfo.getMobile());
        gJformInfoVo.setXyzzjhm(ctmInfo.getCertNo());
        gJformInfoVo.setXyzdz(ctmInfo.getCtmAddr());
        gJformInfoVo.setXyzzjlx(getCertType(ctmInfo.getCertType()));
        gJformInfoVo.setBzsl(pjInfoBean.getMeterNum());
        gJformInfoVo.setRemark(pjInfoBean.getComments());
        gJformInfoVo.setDbrxm(pjInfoBean.getAgentMan());
        gJformInfoVo.setDbrzjlx(getCertType(pjInfoBean.getAgentCertType()));
        gJformInfoVo.setDbrdh(pjInfoBean.getAgentLinkTel());
        gJformInfoVo.setDbrzjhm(pjInfoBean.getAgentCertNo());
        gJformInfoVo.setDbrdz(null);
        gJformInfoVo.setJffs(getPayWay(accountInfo.getPayWay()));//缴费方式
        gJformInfoVo.setJyzzxdk(0);//旧业主注销代扣，默认否
        gJformInfoVo.setOrdertype(1);//业务报装类型，1代表用水
        gJformInfoVo.setSlbh(pjInfoBean.getId());//受理编号
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        gJformInfoVo.setApplytime(dateFormat.format(new Date()));//申请时间

        jsonObject.put("projectInfo",new HashMap<>());//项目信息模型
        jsonObject.put("subProInfo",gJsubProInfoVo);//工程信息模型
        jsonObject.put("sqrInfo",gJsqrInfoVo);//申请人信息模型
//        jsonObject.put("jbrInfo",gJjbrinfoVo);//经办人信息模型
        jsonObject.put("applyInfoCl",new HashMap<>());//申请材料数据模型
        jsonObject.put("formInfo",gJformInfoVo);//表单数据模型
        jsonObject.put("itemJson",itemJson);//事项信息模型
//        jsonObject.put("companyId", "C021017002");
        jsonObject.put("companyId",userInfo.getCurWaterCode());//水司编号
        logger.info("信宜报装同步至工建数据封装:"+pjInfoBean.getId()+"请求参数:"+JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue));
        System.out.println("信宜报装同步至工建数据封装:"+pjInfoBean.getId()+"请求参数:"+JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue));
        return JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue);
    }



    /**
     * 审批及办结数据封装
     */
    public static JSONObject ZjAcceptanceInfo(Integer sfwx, String opStatus, PjInfoBean pjInfoBean, UserInfoBean userInfo, SessionFactory factory){
        //根据id查询工建受理编号
        PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
        String slbh = pjInfoMapper.selectGcjsSlbhById(pjInfoBean.getId());
        String bjjg = "", nodecode = "", zt = "", sphj = "" ;String  spyj ="";
        //设置每种业务状态对应的不同的审批系统参数
        if (opStatus.equals("1")) {
            nodecode = "1000";
            zt = "1";
            sphj = "受理";
            spyj = "通过";
        } else if (opStatus.equals("2")) {
            nodecode = "1000";
            zt = "1";
            sphj = "受理";
            spyj = "通过";
        } else if (opStatus.equals("3")) {
            bjjg = "6";
            nodecode = "9000";
            zt = "1";
            sphj = "办结";
            spyj = "通过";
        } else if (opStatus.equals("4")) {
            bjjg = "7";
            nodecode = "9000";
            zt = "2";
            sphj = "办结";
            spyj = "不通过";
        }
        //当前时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //参数封装
        JSONObject gjson = new JSONObject();
        gjson.put("zt", zt);
        gjson.put("slbh", slbh);
        gjson.put("spsj", dateFormat.format(new Date()));
        gjson.put("bjjg", bjjg);
        gjson.put("nodecode", nodecode);
        gjson.put("sphj", sphj);
        gjson.put("spyj", spyj);
        gjson.put("blr", "信宜粤海水务有限公司");
        gjson.put("companyId", userInfo.getCurWaterCode());
//        gjson.put("companyId", "C021017002");
        if (sfwx!=null) {
            gjson.put("sfwx", sfwx);//是否外线,0否，1是
            gjson.put("sfxyjf", 1);//是否缴费，默认写死是
        }
        if (opStatus.equals("2")) {
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
            PjCollectionQuery pjCollectionQuery = new PjCollectionQuery();
            pjCollectionQuery.setProjectCode(pjInfoBean.getBillNo());
            List<PjCollection> pjCollections = pjCollectionMapper.getList(pjCollectionQuery);
            //如果是第二次受理，即传给工建系统的第三个时间节点，则传缴费时间，缴费时间不存在则传当前时间
            if (pjCollections!=null){
                String paymentDate = pjCollections.get(0).getPaymentDate();
                gjson.put("jfsj",paymentDate!=null?paymentDate:dateFormat.format(new Date()));//缴费时间
            }else {
                gjson.put("jfsj", dateFormat.format(new Date()));//缴费时间
            }
        }
        logger.info("信宜报装审批同步至工建数据封装:数据id"+pjInfoBean.getId()+"请求参数:"+JSONObject.toJSONString(gjson, SerializerFeature.WriteMapNullValue));
        System.out.println("信宜报装审批同步至工建数据封装:"+pjInfoBean.getId()+"请求参数:"+JSONObject.toJSONString(gjson, SerializerFeature.WriteMapNullValue));
        return gjson;
    }



    /**
     * 身份证类型转换
     */
    public static String getCertType(String oldCertType){
        switch (oldCertType){
            case "1"://身份证
                return "10";
            case "2"://港澳台通行证
                return "14";
            case "3"://护照
                return "20";
            case "4"://回乡证
                return "80";
            case "5"://居住证
                return "80";
            case "6"://营业执照
                return "51";
            case "7"://其他
                return "80";
            case "8"://统一社会信用代码
                return "49";
            case "9"://港澳台身份证
                return "16";
            case "10"://低保证号
                return "80";
            default:
                return "80";
        }
    }


    /**
     * 身份证类型转换
     */
    public static int getWaterType(String oldWaterType){
        switch (oldWaterType){
            case "1":// 用水报装-居民
                return 7;
            case "2"://水表迁移
                return 6;
            case "3"://水表扩缩
                return 5;
            case "4"://管道维修
                return 2;
            case "5"://用水报装-企业
                return 8;
            default:
                return 2;
        }
    }

    /**
     * 缴费方式转换
     */
    public static int getPayWay(String oldPayWay){
        switch (oldPayWay){
            case "1":// 代扣
                return 2;
            case "2"://托收
                return 2;
            case "3"://现金
                return 1;
            default:
                return 1;
        }
    }

    /**
     * 信宜对接工建系统测试专用
     */
//    public static void main(String[] args) {
//        String opStatus ="2";
//        String slbh ="MM202308160010";
//        String time ="2023-08-11 15:23:36";
//        String sfwx = null;
//
//
//        if ("1".equals(opStatus)){
//             sfwx = "0";
//        }
//        String bjjg = "", nodecode = "", zt = "", sphj = "" ;String  spyj ="";
//        //设置每种业务状态对应的不同的审批系统参数
//        if (opStatus.equals("1")) {
//            nodecode = "1000";
//            zt = "1";
//            sphj = "受理";
//            spyj = "通过";
//        } else if (opStatus.equals("2")) {
//            nodecode = "1000";
//            zt = "1";
//            sphj = "受理";
//            spyj = "通过";
//        } else if (opStatus.equals("3")) {
//            bjjg = "6";
//            nodecode = "9000";
//            zt = "1";
//            sphj = "办结";
//            spyj = "通过";
//        } else if (opStatus.equals("4")) {
//            bjjg = "7";
//            nodecode = "9000";
//            zt = "2";
//            sphj = "办结";
//            spyj = "不通过";
//        }
//        //当前时间
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        //参数封装
//        JSONObject gjson = new JSONObject();
//        gjson.put("zt", zt);
//        gjson.put("slbh", slbh);
//        gjson.put("spsj", time);
////        gjson.put("spsj", dateFormat.format(new Date()));
//        gjson.put("bjjg", bjjg);
//        gjson.put("nodecode", nodecode);
//        gjson.put("sphj", sphj);
//        gjson.put("spyj", spyj);
//        gjson.put("blr", "信宜粤海水务有限公司");
//        gjson.put("companyId", "C021017002");
//        if (sfwx!=null) {
//            gjson.put("sfwx", sfwx);//是否外线,0否，1是
//            gjson.put("sfxyjf", 1);//是否缴费，默认写死是
//        }
//        if (opStatus.equals("2")) {
////            gjson.put("jfsj", dateFormat.format(time));//缴费时间
//            gjson.put("jfsj", time);//缴费时间
//
//        }
//        System.out.println(gjson);
//    }
}
